# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json
from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.requestFieldMapDBField import requestFieldMapToDBField


class AdminRoleMenuDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertAdminRoleMenu(self, info):
        """
        添加admin_role_menu信息
        :param info:
        :return:
        """
        roleId = info['roleId']
        menuTitle = info['roleTitle']
        actionKey = info['actionKey']

        sql = "insert into zzh_admin_role_menu (role_id, menu_title, action_key) VALUES " \
              "(%s,%s,%s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [roleId, menuTitle, actionKey])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findAdminRoleMenuById(self, roleMenuId):
        """
        根据ID查询admin_role_menu信息
        :param roleMenuId:
        :return:
        """
        sql = "select * from zzh_admin_role_menu WHERE id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [roleMenuId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.admin_role_menu_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def findAdminRoleMenuByCondition(self, **kwargs):
        """
        view_role_menu表的条件查询
        :param kwargs:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        fieldMap = requestFieldMapToDBField.viewRoleMenuFieldMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM view_role_menu WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM view_role_menu WHERE " + whereSentence
        rs = cur.execute(sql)

        jsons = None
        if rs:
            rsSet = cur.fetchall()
            list0 = MyPaging().getPage(rs, pageIndex,pageSize, rsSet)
            jsons = Table2Json.view_role_menu_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons, rs

    def deleteRoleMenu(self, idArray):
        """
        根据ID删除信息，可以传递一个或者多个ID，ID以数组形式传递
        :param idArray: ID构成的数组，如：[1,2]
        :return:
        """
        sql = "delete from zzh_admin_role_menu WHERE id=%s"
        cur = self.conn.cursor()
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateRoleMenu(self, info):
        """
        更新admin_role_menu
        :param info:
        :return:
        """
        roleMenuId = info['roleMenuId']
        roleId = info['roleId']
        menuTitle = info['roleTitle']
        actionKey = info['actionKey']

        sql = "update zzh_admin_role_menu set role_id=%s, menu_title=%s,action_key=%s WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [roleId, menuTitle, actionKey, roleMenuId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectRoleMenu(self, rmId):
        """
        根据ID查询，返回元组
        :param rmId:
        :return: data tuple
        """
        sql = "select * from zzh_admin_role_menu WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [rmId])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr

    def add_role_menus(self, addArr):
        sql = "insert into zzh_admin_role_menu (role_id, menu_title, action_key) VALUES " \
              "(%s, %s, %s)"
        cur = self.conn.cursor()
        rs = 0
        for arr in addArr:
            rs = cur.execute(sql, [arr['roleId'], arr['menuTitle'], arr['actionKey']])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

